package com.xinzhi.dao;

import com.xinzhi.entity.Cart;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 对购物车修改类
 */
public class CartDao {
    /**
     * 添加购物车
     *
     * @param cart
     * @return
     */
    public int insertCart(Cart cart) {
        Connection conn = null;
        PreparedStatement stat = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            String sql = "insert into cart values (?,?,?,?)";
            stat = conn.prepareStatement(sql);
            stat.setString(1, cart.getName());
            stat.setInt(2, cart.getPrice());
            stat.setInt(3, cart.getNumber());
            stat.setString(4, cart.getUser().getUsername());
            count = stat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(null, stat, conn);
        }
        return count;
    }

    /**
     * 判断用户选择的商品是否重复
     *
     * @param cart
     * @return
     */
    public int selectGoods(Cart cart) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = 0;
        try {

            conn = BaseDao.getConn();
            //创建sql语句的Statement对象
            String sql = "select * from cart where name= ? and username = ?";
            stat = conn.prepareStatement(sql);
            //对sql语句进行赋值
            stat.setString(1, cart.getName());
            stat.setString(2, cart.getUser().getUsername());
            //sql预加载
            rs = stat.executeQuery();
            if (rs.next()) {
                count = 1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return count;
    }

    /**
     * 判断结束后选择修改
     *
     * @param cart
     * @return
     */
    public int updateCart(Cart cart) {
        Connection conn = null;
        PreparedStatement stat = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            String sql = "update cart set number = (number + ?) where name = ? and username = ?";
            stat = conn.prepareStatement(sql);
            stat.setInt(1, cart.getNumber());
            stat.setString(2, cart.getName());
            stat.setString(3, cart.getUser().getUsername());
            count = stat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(null, stat, conn);
        }
        return count;
    }

    /**
     * 4.查看购物车
     *
     * @param user
     * @return
     */
    public ArrayList<Cart> payment(String user) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        //数组集合，为了能够放下所有的信息
        ArrayList<Cart> carts = new ArrayList<>();
        try {
            conn = BaseDao.getConn();
            //创建sql语句的Statement对象
            String sql = "select * from cart where username= ? ";
            stat = conn.prepareStatement(sql);
            //对sql语句进行赋值
            stat.setString(1, user);
            //sql预加载
            rs = stat.executeQuery();
            while (rs.next()) {
                Cart cart = new Cart();
                cart.setName(rs.getString("name"));
                cart.setPrice(rs.getInt("price"));
                cart.setNumber(rs.getInt("number"));
                cart.getUser().setUsername(rs.getString("username"));
                carts.add(cart);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stat, conn);
        }
        return carts;
    }

    /**
     *移除商品
     */
    public int remove(String name,String username){
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = BaseDao.getConn();
            // 2. 创建操作sql语句的Statement对象
            String sql = "delete  from cart where name= ? or username = ? ";
            // sql预加载
            stat = conn.prepareStatement(sql);
            stat.setString(1, name);
            stat.setString(2, username);
            count= stat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs,stat,conn);
        }
        return count;

    }

    /**
     *清空购物车
     */
    public int delete(String username){
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int count = -1;
        try {
            conn = BaseDao.getConn();
            // 2. 创建操作sql语句的Statement对象
            String sql = "delete  from cart where username = ? ";
            // sql预加载
            stat = conn.prepareStatement(sql);
            stat.setString(1,username);
            count = stat.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeAll(rs,stat,conn);
        }
        return count;

    }

}
